package net.lovesrc.allin.jobs.backup;

import net.lovesrc.allin.biz.stl.StlService;
import net.lovesrc.allin.biz.user.UserService;
import net.lovesrc.allin.jobs.base.BaseJob;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;

import java.util.Date;
import java.util.concurrent.TimeUnit;

//@Component
@Profile("live")
public class StlAutoNewSellJob extends BaseJob {

    @Autowired
    private StlService stlService;

    @Override
    public void runOnce() {
        if (stlService.isNotJyTime()) {
            return;
        }
        Integer newQty = jdbcTemplate.queryForObject("select count(1) from t_stl_sell where status = 'Waiting_Buyer_Pay'", int.class);
        if (newQty >= 4) {
            return;
        }
        Integer autoNewSellIntervalSeconds = tryGetProperties(int.class, 180, "STL_Auto_New_Sell_Interval_Seconds");
        Date latestSellDate = jdbcTemplate.queryForObject("select insertTime from t_stl_sell order by id desc limit 1", Date.class);
        if (new Date().getTime() - latestSellDate.getTime() >= autoNewSellIntervalSeconds * DateUtils.MILLIS_PER_SECOND) {
            stlService.newSell(UserService.SYSTEM);
        }
    }

    @Override
    protected long intervalMilliseconds() {
        return TimeUnit.SECONDS.toMillis(5);
    }

}
